package com.ycdx.zwzc.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.log4j.Logger;

import com.ycdx.zwzc.pojo.Reason;
import com.ycdx.zwzc.utils.DBPool;

public class ReasonImpl {
	
	static Logger logger = Logger.getLogger(ReasonImpl.class.getName());
	
	@SuppressWarnings("unchecked")
	public static List<Reason> queryAllReasons(){
		List<Reason> reasonList = new ArrayList<Reason>();
		String sql = "select * from tb_reason;";
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			reasonList = (List<Reason>)qr.query(conn, sql, new BeanListHandler(Reason.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block	
			logger.error("ReasonImpl.class-queryAllReasons()-数据库操作失败！");
			e.printStackTrace();
			
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ReasonImpl.class-queryAllReasons()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return reasonList;
	}
	
	
	/**
	 * 添加入库
	 * @param req
	 * @return
	 */
	public static int addReason(Reason reason){
		int flag = 0;
		String sql = "insert into tb_reason(" +
				"name) values(" +
				"?);";	
		Object[] params = {reason.getName()};
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			flag = qr.update(conn, sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("ReasonImpl.class-addReason()-数据库操作失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ReasonImpl.class-addReason()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
	
	/**
	 * 根据ID获取指定对象
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Reason getReasonById(String id){
		//System.out.println(id);
		String sql = "select * from tb_reason where id=?";
		Object[] params = {id};
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();		
		Reason reason = new Reason();
		try {
			reason = (Reason) qr.query(conn,sql, new BeanHandler(Reason.class), params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("ReasonImpl.class-getReasonById()-数据库操作失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ReasonImpl.class-getReasonById()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return reason;
	}
	
	/**
	 * 更新指定对象
	 * @param reason
	 * @return
	 */
	public static int update(Reason reason){
		int flag = 0;
		String sql = "update tb_reason set name=? where id=?";
		Object[] params = {reason.getName(),reason.getId()};
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			flag = qr.update(conn, sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("ReasonImpl.class-update()-数据库操作失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ReasonImpl.class-update()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return flag;
	}
	
	/**
	 * 删除指定对象
	 * @param reason
	 * @return
	 */
	public static int deleteReason(String id){
		String sql = "delete from tb_reason where id=?";
		Object[] params = {id};
		Connection conn = DBPool.getInstance().getConnection();
		QueryRunner qr = new QueryRunner();
		
		int flag = 0;
		try {
			flag = qr.update(conn,sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error("ReasonImpl.class-deleteReason()-数据库操作失败！");
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("ReasonImpl.class-deleteReason()-连接关闭失败！");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
}
